package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@Api(tags = "套餐管理")
@RequestMapping("/admin/setmeal")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    /**
     * 新增套餐
     * */
    @CacheEvict(cacheNames = "setmeal",key = "#dto.categoryId")
    @ApiOperation("新增套餐")
    @PostMapping
    public Result add(@RequestBody SetmealDTO dto) {
        log.info("套餐新增{}",dto);
        setmealService.add(dto);
        return Result.success();
    }
    /**
     * 套餐分页查询
     * */
    @ApiOperation("分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO dto) {
        log.info("条件分页查询{}",dto);
        PageResult page = setmealService.getPage(dto);
        return Result.success(page);
    }
    /**
     * 根据id查询
     * */
    @ApiOperation("根据id查询")
    @GetMapping("/{id}")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询：{}",id);
        SetmealVO setmealVO = setmealService.getById(id);
        return Result.success(setmealVO);
    }
    /**
     * 批量删除套餐
     * */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("批量删除套餐")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids) {
        log.info("批量删除套餐：{}",ids);
        setmealService.delete(ids);
        return Result.success();
    }
    /**
     * 起售停售套餐
     * */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @ApiOperation("起售停售套餐")
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status, Long id) {
        log.info("套餐状态修改id:{},status:{}",id,status);
        setmealService.updateStatus(status,id);
        return Result.success();
    }
    /**
     * 修改套餐
     * */
    @CacheEvict(cacheNames = "setmeal",allEntries = true)
    @PutMapping
    @ApiOperation("修改套餐")
    public Result update(@RequestBody SetmealDTO dto) {
        log.info("修改套餐信息：：{}",dto);
        setmealService.update(dto);
        return Result.success();
    }
}
